package com.jixiantubu.yueban;

import image.SmartImageView;
import java.io.File;
import view.Circleimage;
import com.example.jixiantubu.R;
import com.jixiantubu.bean.Ac_GeoCoder_Bean;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class YueBan_MuLu_FaQi extends Activity implements OnClickListener {
	private SmartImageView ac_fq_add_img;
	private ImageView ac_fq_back;
	private Button ac_faqi_btn;
	private Intent intent;
	private Byte[] data;
	private Bitmap bitmap;
	private Uri uri;
	private String url = "http://" + MyApplication.getIP()
			+ ":8080/Jinnang/YueBan_FaQi";
	private String filename = "AC" + System.currentTimeMillis() + ".jpg";
	private EditText ac_title_fq, ac_time_fq, ac_lastTime_fq, ac_venue_fq,
			ac_destination_fq, ac_num_fq, ac_contact_fq, ac_content_fq;
	private String ac_pic_path, ac_title, ac_time, ac_lastTime, ac_venue,
			ac_venue_longitude, ac_venue_latitude, ac_destination,
			ac_destination_longitude, ac_destination_latitude, ac_num,
			ac_contact, ac_content;
	private MyApplication application;
	private Ac_GeoCoder_Bean vBean, dBean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.ac_faqi);
		initView();

	}

	private void initView() {
		ac_fq_back = (ImageView) findViewById(R.id.ac_fq_back);
		ac_faqi_btn = (Button) findViewById(R.id.ac_faqi_btn);
		ac_title_fq = (EditText) findViewById(R.id.ac_title_fq);
		ac_time_fq = (EditText) findViewById(R.id.ac_time_fq);
		ac_lastTime_fq = (EditText) findViewById(R.id.ac_lastTime_fq);
		ac_venue_fq = (EditText) findViewById(R.id.ac_venue_fq);
		ac_destination_fq = (EditText) findViewById(R.id.ac_destination_fq);
		ac_num_fq = (EditText) findViewById(R.id.ac_num_fq);
		ac_contact_fq = (EditText) findViewById(R.id.ac_contact_fq);
		ac_content_fq = (EditText) findViewById(R.id.ac_content_fq);
		ac_fq_add_img = (SmartImageView) findViewById(R.id.ac_fq_add_img);

		ac_fq_back.setOnClickListener(this);
		ac_faqi_btn.setOnClickListener(this);
		ac_venue_fq.setOnClickListener(this);
		ac_destination_fq.setOnClickListener(this);
		ac_fq_add_img.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog();

			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		switch (requestCode) {
		case 1:
			startPhotoZoom(uri);// 剪裁
			break;
		case 2:
			if (data != null) {
				Uri uri = data.getData();// 获取本地地址得到图片
				startPhotoZoom(uri);// 剪裁
			}
			break;
		case 3:
			// 上传
			ac_pic_path = this.getExternalCacheDir() + "/" + filename;
			Bitmap bitmap1 = BitmapFactory.decodeFile(this
					.getExternalCacheDir() + "/" + filename);
			//
			Circleimage image2 = new Circleimage();
			ac_fq_add_img.setImageBitmap(image2.toRoundBitmap(bitmap1));
			upload();
			break;
		case 4:
			if (resultCode == RESULT_OK) {
				vBean = (Ac_GeoCoder_Bean) data.getSerializableExtra("abean");
				ac_venue_fq.setText(vBean.getAddress());
			}
			break;
		case 5:
			if (resultCode == RESULT_OK) {
				dBean = (Ac_GeoCoder_Bean) data.getSerializableExtra("abean");
				ac_destination_fq.setText(dBean.getAddress());
			}
			break;
		default:
			break;
		}
	}

	public void upload() {
		ac_title = ac_title_fq.getText().toString();
		ac_time = ac_time_fq.getText().toString();
		ac_lastTime = ac_lastTime_fq.getText().toString();

		ac_venue = ac_venue_fq.getText().toString();
		ac_venue_longitude = String.valueOf(vBean.getLongitude());
		ac_venue_latitude = String.valueOf(vBean.getLatitude());

		ac_destination = ac_destination_fq.getText().toString();
		ac_destination_longitude = String.valueOf(dBean.getLongitude());
		ac_destination_latitude = String.valueOf(dBean.getLongitude());
		ac_num = ac_num_fq.getText().toString();
		ac_contact = ac_contact_fq.getText().toString();
		ac_content = ac_content_fq.getText().toString();
		RequestParams params = new RequestParams();
		params.addBodyParameter("user_id", MyApplication.user_id);
		params.addBodyParameter("ac_pic_path", new File(ac_pic_path));
		params.addBodyParameter("ac_title", ac_title);
		params.addBodyParameter("ac_time", ac_time);
		params.addBodyParameter("ac_lastTime", ac_lastTime);
		params.addBodyParameter("ac_venue", ac_venue);
		params.addBodyParameter("ac_venue_longitude", ac_venue_longitude);
		params.addBodyParameter("ac_venue_latitude", ac_venue_latitude);
		params.addBodyParameter("ac_destination", ac_destination);
		params.addBodyParameter("ac_destination_longitude",
				ac_destination_longitude);
		params.addBodyParameter("ac_destination_latitude",
				ac_destination_latitude);
		params.addBodyParameter("ac_num", ac_num);
		params.addBodyParameter("ac_contact", ac_contact);
		params.addBodyParameter("ac_content", ac_content);
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {
			@Override
			public void onFailure(HttpException arg0, String arg1) {

			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
			}
		});

	}

	// 弹框
	private void showDialog() {
		View view = getLayoutInflater().inflate(R.layout.photo_choose_dialog,
				null);
		Button button = (Button) view.findViewById(R.id.wo_take_photo);
		Button button1 = (Button) view.findViewById(R.id.wo_select);
		Button button2 = (Button) view.findViewById(R.id.wo_quxiao);
		final Dialog dialog = new Dialog(this,
				R.style.transparentFrameWindowStyle);
		dialog.setContentView(view, new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		Window window = dialog.getWindow();
		// 设置显示动画
		window.setWindowAnimations(R.style.main_menu_animstyle);
		WindowManager.LayoutParams wl = window.getAttributes();
		wl.x = 0;
		wl.y = getWindowManager().getDefaultDisplay().getHeight();
		// 以下这两句是为了保证按钮可以水平满屏
		wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
		wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;

		// 设置显示位置
		dialog.onWindowAttributesChanged(wl);
		// 设置点击外围解散
		dialog.setCanceledOnTouchOutside(true);

		dialog.show();

		// 拍照
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				System.out.println(filename);
				uri = Uri.fromFile(new File(Environment
						.getExternalStorageDirectory(), filename));
				intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
						Environment.getExternalStorageDirectory(), filename)));
				Log.e("urits", uri.toString());
				startActivityForResult(intent, 1);

			}
		});

		// 图册获取
		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				intent = new Intent(Intent.ACTION_PICK, null);
				intent.setDataAndType(
						MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
				startActivityForResult(intent, 2);

			}
		});
		// 取消按钮
		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
	}

	// 图像裁剪
	public void startPhotoZoom(Uri uri) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1.5);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 300);
		intent.putExtra("outputY", 300);
		intent.putExtra("return-data", true);
		intent.putExtra("output",
				Uri.fromFile(new File(this.getExternalCacheDir(), filename)));
		startActivityForResult(intent, 3);
	}

	@Override
	public void onClick(View v) {
		Log.e("qqqqq", "" + v.getId());
		Log.e("set", "" + R.id.ac_venue_fq);
		switch (v.getId()) {
		case R.id.ac_fq_back:
			finish();
			break;
		case R.id.ac_faqi_btn:
			upload();
			Intent i1 = new Intent(YueBan_MuLu_FaQi.this,
					YueBan_MuLu_QuanBu.class);
			startActivity(i1);
			break;
		case R.id.ac_venue_fq:

			Intent i2 = new Intent(YueBan_MuLu_FaQi.this, YueBan_Map.class);
			startActivityForResult(i2, 4);

			break;
		case R.id.ac_destination_fq:

			Intent i3 = new Intent(YueBan_MuLu_FaQi.this, YueBan_Map.class);
			startActivityForResult(i3, 5);
			break;
		default:
			break;
		}
	}
}
